
//轮播图

const carousel = async () => {

  const { data: res } = await axios.get('/index/index')


  $(".lbt").html(res.data.banner.map(item => {
    return `
      <div>
        <a target="_blank" href="${item.advimglink}" style="height:340px">
        <img class="thumb" src="http://124.223.14.236:8060/${item.advimgsrc}" >
        <h4>${item.advimgdesc}</h4>
        </a>
      </div>
          `

  }).join(''))
  layui.use('carousel', function () {
    let carousel = layui.carousel;
    //建造实例
    carousel.render({
      elem: '#test1'
      , width: '100%' //设置容器宽度
      // , arrow: 'always' //始终显示箭头
      , interval: 2000
    });
  });

}
carousel()


const hot = async () => {

  const { data: res } = await axios.get('/index/hot')
  // console.log(res)
  $('.hot').html(res.data.list.map(item => {
    return `
      <li class="item">
        <a target="_self" href="#/" id='tzshow' data-id=${item.id}><img src="http://124.223.14.236:8060/${item.pic}" class="thumb" />${item.title}</a>
      </li>
`
  }).join(''))

}
hot()


let options = {
  page: 1,
  limit: 10,
}
const newList = async () => {


  const { data: res } = await axios.get('/index/new', { params: options })
  page = res.data.list.currentPage
  limit = res.data.list.pageSize
  let count = res.data.list.count
  // console.log(page);

  renderPage(count)

  $('#title').html(
    `
      <small class="pull-right">24小时更新：${res.data.num_day}篇 &nbsp; &nbsp; 一周更新：${res.data.num_week}篇</small><strong>最近更新</strong>
    `
  )


  $('.new').html(res.data.list.data.map(item => {
    // console.log(item);
    return `
    <article class="excerpt excerpt-one">
            <header>
              <a class="cat label label-important" href="#/">热门<i class="label-arrow"></i></a>
              <h2>
                <a target="_self" href="show.html?${item.id}" title="阿里云拼团推荐服务器">${item.title}</a>
              </h2>
            </header>
            <p class="text-muted time">
              <a href="">${item.author}</a> 发布于 ${formatTime(item.create_date)}
            </p>
            <p class="focus">
              <a target="_blank" href="http://blog.weknow.cn/474.shtml" class="thumbnail">
                <span class="item"><span class="thumb-span"><img src="http://124.223.14.236:8060/${item.pic}"
                      class="thumb" /></span></span></a>
            </p>
            <p class="note">${item.description}</p>
            <p class="text-muted views">
              <span class="post-views">阅读(${item.click})</span>
              <span class="post-comments">评论(0)</span>
              <a href="javascript:;" class="post-like">
                <i class="fa fa-thumbs-up"></i>赞 (<span>${item.lovenum}</span>)
              </a>
              <span class="post-tags">
                <a href="" rel="tag"></a>
                <a href="" rel="tag"></a></span>
            </p>
          </article>
    `
  }).join(''))


  // 分页
  function renderPage(count) {
    let laypage = layui.laypage;
    laypage.render({
      elem: 'page',
      count, // 数据总数，从后端得到
      theme: '#ff7267',
      limit: options.limit,
      curr: options.page,
      layout: ['prev', 'page', 'next', 'count'],
      jump: function (obj, first) {
        // console.log(obj.curr); // 得到当前页，以便向服务端请求对应页的数据。
        // console.log(obj.limit); // 得到每页显示的条数

        options.page = obj.curr
        options.limit = obj.limit

        // 首次不执行
        if (!first) {
          // do something
          newList()
        }
      }
    });
  }


}
newList()

